package org.ndot.java.util.concurrent.bank;

import java.util.ArrayList;
import java.util.List;

/**
 * @公司名称: 小蚂蚁学堂
 * 
 * @项目名称: temp
 * 
 * @类名名称: org.ndot.bank.BankOfChina.java
 * 
 * @作者姓名: SunJincheng
 * 
 * @软件版本: v1.0
 * 
 * @创建日期: 2010-4-13下午04:26:27
 */
public class BankOfChina {
	private List<Consumer> comm_list;
	private List<Consumer> rap_list;
	private List<Consumer> vip_list;

	private Operator win_comm_1;
	private Operator win_comm_2;
	private Operator win_comm_3;
	private Operator win_comm_4;

	private Operator win_rap;
	private Operator win_vip;

	@SuppressWarnings("unchecked")
	public BankOfChina() {

		comm_list = new ArrayList<Consumer>();
		rap_list = new ArrayList<Consumer>();
		vip_list = new ArrayList<Consumer>();

		win_comm_1 = new Operator(comm_list);
		win_comm_1.setName("win_comm_1");
		win_comm_2 = new Operator(comm_list);
		win_comm_2.setName("win_comm_2");
		win_comm_3 = new Operator(comm_list);
		win_comm_3.setName("win_comm_3");
		win_comm_4 = new Operator(comm_list);
		win_comm_4.setName("win_comm_4");

		win_rap = new Operator(rap_list, comm_list);
		win_rap.setName("win_rap");
		win_vip = new Operator(vip_list, comm_list);
		win_vip.setName("win_vip");
	}

	public void start() {
		System.out.println("中国银行开始营业.......");
		this.win_comm_1.start();
		this.win_comm_2.start();
		this.win_comm_3.start();
		this.win_comm_4.start();
		this.win_rap.start();
		this.win_vip.start();
	}

	/**
	 * @param args
	 *            void
	 */
	public static void main(String[] args) {
		try {
			BankOfChina bank = new BankOfChina();

			GenConsumer gen_comm = new GenConsumer(bank.comm_list, 1);
			GenConsumer gen_rap = new GenConsumer(bank.rap_list, 3);
			GenConsumer gen_vip = new GenConsumer(bank.vip_list, 6);

			gen_comm.start();
			gen_rap.start();
			gen_vip.start();

			bank.start();

			Thread.sleep(60000);

			gen_comm.setTag(false);
			gen_rap.setTag(false);
			gen_vip.setTag(false);

			bank.win_comm_1.setEnd(true);
			bank.win_comm_2.setEnd(true);
			bank.win_comm_3.setEnd(true);
			bank.win_comm_4.setEnd(true);
			bank.win_rap.setEnd(true);
			bank.win_vip.setEnd(true);

		} catch (Exception e) {
			e.printStackTrace();
		}

	}
}

class GenConsumer extends Thread {
	private List<Consumer> consumer_list;
	private int speed = 6;
	private boolean tag = true;
	private int i=1;
	private String msg = "";

	public GenConsumer(List<Consumer> consumer_list, int speed) {
		this.consumer_list = consumer_list;
		this.speed = speed;
	}

	@Override
	public void run() {
		try {
			switch (speed) {
			case 6: {
				msg = BankParam.printInfo(BankParam.TYPE_VIP);
				while (tag) {
					consumer_list.add(new Consumer(i++, BankParam.TYPE_VIP,
							getDoTime(speed)));
					Thread.sleep(speed * 1000);
				}
				System.err.println("共计完成了 " + --i + " 个 " + msg + " 类型业务......");
				break;
			}
			case 3: {
				msg = BankParam.printInfo(BankParam.TYPE_RAP);
				while (tag) {
					consumer_list.add(new Consumer(i++, BankParam.TYPE_RAP,
							getDoTime(speed)));
					Thread.sleep(speed * 1000);
				}
				System.err.println("共计完成了 " + --i + " 个 " + msg + " 类型业务......");
				break;
			}
			case 1: {
				msg = BankParam.printInfo(BankParam.TYPE_COMM);
				while (tag) {
					consumer_list.add(new Consumer(i++, BankParam.TYPE_COMM,
							getDoTime(speed)));
					Thread.sleep(speed * 1000);

				}
				System.err.println("共计完成了 " + --i + " 个 " + msg + " 类型业务......");
				break;
			}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	public boolean isTag() {
		return tag;
	}

	public void setTag(boolean tag) {
		this.tag = tag;

	}

	private int getDoTime(int type) {
		double tem = (Math.random() * BankParam.MAX_TIME);
		tem = tem > BankParam.MIN_TIME ? tem : BankParam.MIN_TIME;
		if (3 == type)
			return BankParam.MIN_TIME;
		return (int) tem;
	}
}
